package logdroid.client; 

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import com.example.helloandroid.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class HelloAndroid2 extends Activity {
     
	private String recipient = "meinserver";
	private int sendPort = 6001;
	private int listenPort = 6000;
	private Sender sender;
	private Listener listener;
	
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       TextView tv = new TextView(this);
       tv.setText(callServer());
       setContentView(tv);
       
       
       sender = new Sender(recipient, sendPort);
       listener = new Listener(listenPort, sender);
       Thread t0 = new Thread( listener );
       t0.run();
   }
   
   private String callServer() {
	    try {
	        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
	            NetworkInterface intf = en.nextElement();
	            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
	                InetAddress inetAddress = enumIpAddr.nextElement();
	                if (!inetAddress.isLoopbackAddress()) {
	                    String ip = inetAddress.getHostAddress().toString();
//	                    sender.send("command:setRecipient:" + ip);
	                    return inetAddress.getHostAddress();
	                }
	            }
	        }
	    } catch (SocketException ex) {
	        Log.e("getInetIP",ex.toString());
	    }
	    return "0";
   }
   
}